package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.CmactivationCodeDto;
import com.dingreading.cloud.soms.entity.CmactivationCode;
import com.dingreading.cloud.soms.entity.table.CmactivationCodeTableDef;
import com.dingreading.cloud.soms.mapper.CmactivationCodeMapper;
import com.dingreading.cloud.soms.service.CmactivationCodeService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 门店激活码详情表 服务层实现。
 *
 * @author EDY
 * @since 2025-10-29
 */
@Service
public class CmactivationCodeServiceImpl extends BaseServiceImpl<CmactivationCodeMapper, CmactivationCode, CmactivationCodeTableDef> implements CmactivationCodeService {

    @Override
    protected CmactivationCodeTableDef getTable() {
        return CmactivationCodeTableDef.cmactivationCode;
    }

    @Override
    public R<Object> pageList(PageUtil pageUtil, String corpUid, String storeUid, String staffUid, String staffName) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("门店员工字段缺少请检查！");
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.corpUid.eq(corpUid))
                .and(table.storeUid.eq(storeUid));
        QueryWrapper wrapper = new QueryWrapper()
                .where(condition)
                .orderBy(table.id.desc());
        Page<CmactivationCodeDto> cmactivationCodeDtoPage = getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, CmactivationCodeDto.class);
        PageDto<CmactivationCodeDto> pageDto = new PageDto<>();
        pageDto.setTotal(cmactivationCodeDtoPage.getTotalRow());
        pageDto.setList(cmactivationCodeDtoPage.getRecords());
        return R.ok(pageDto);
    }
    @Override
    public Long getCodeNum(String corpUid, String storeUid, String activationTypeUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.corpUid.eq(corpUid))
                .and(table.storeUid.eq(storeUid))
                .and(table.activationTypeUid.eq(activationTypeUid))
                .and(table.status.eq(0));
        return count(condition);
    }

    @Override
    public Boolean getCodeForActivate(
            String corpUid, String storeUid, String activationTypeUid,
            String orderCode, String serviceUid, String memberUid, String memberName, LocalDateTime usedTime,
            String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.corpUid.eq(corpUid))
                .and(table.storeUid.eq(storeUid))
                .and(table.activationTypeUid.eq(activationTypeUid))
                .and(table.status.eq(0));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());
        CmactivationCode cmactivationCode = getMapper().selectOneByQuery(wrapper);
        if (cmactivationCode == null)
            return false;
        cmactivationCode.setStatus(1);
        cmactivationCode.setCmorderNo(orderCode);
        cmactivationCode.setCmserviceUid(serviceUid);
        cmactivationCode.setStudentUid(memberUid);
        cmactivationCode.setStudentName(memberName);
        cmactivationCode.setUsedTime(usedTime);
        cmactivationCode.setLastStaffUid(staffUid);
        cmactivationCode.setLastStaffName(staffName);
        return saveOrUpdate(cmactivationCode);
    }


}
